package com.cn.yang.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.cn.yang.model.UserModel;
import com.cn.yang.service.UserService;
import com.cn.yang.service.impl.UserServiceImpl;
import com.cn.yang.util.PageUtil;
import com.cn.yang.util.SortUserUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/back/user/findUser")
public class SelectUserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        String phone = req.getParameter("phone");
        String firstbirthday = req.getParameter("firstbirthday");
        String lastbirthday = req.getParameter("lastbirthday");
        String sex = req.getParameter("sex");
        String hobby = req.getParameter("hobby");
        Integer pageSize = Integer.valueOf(req.getParameter("pageSize"));
        Integer pageNumber = Integer.valueOf(req.getParameter("pageNumber"));
        String sortId = req.getParameter("sortId");
        String sortUserName = req.getParameter("sortUserName");
        String sortSex = req.getParameter("sortSex");
        String sortBirthday = req.getParameter("sortBirthday");
        String sortCreatetime = req.getParameter("sortCreatetime");
        String sortUpdatetime = req.getParameter("sortUpdatetime");
        PageUtil pageUtil = new PageUtil();
        pageUtil.setPageSize(pageSize);
        pageUtil.setPageNumber(pageNumber);
        UserModel userModel = new UserModel();
        userModel.setUsername(username);
        userModel.setPhone(phone);
        userModel.setFirstbirthday(firstbirthday);
        userModel.setLastbirthday(lastbirthday);
        userModel.setSex(sex);
        userModel.setHobby(hobby);
        userModel.setPageUtil(pageUtil);
        SortUserUtil userUtil = new  SortUserUtil();
        userUtil.setSortId(sortId);
        userUtil.setSortUserName(sortUserName);
        userUtil.setSortSex(sortSex);
        userUtil.setSortBirthday(sortBirthday);
        userUtil.setSortCreatetime(sortCreatetime);
        userUtil.setSortUpdatetime(sortUpdatetime);
        userModel.setSortUserUtil(userUtil);

        UserService service = new UserServiceImpl();
        List<UserModel> list = service.findSomeUser(userModel);
        int count = service.countUser();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list",list);
        jsonObject.put("count",count);
        resp.getWriter().println(jsonObject);
    }
}
